﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SeleniumOrderPathRunner {
    class Program {
        static void Main(string[] args) {
            UserInputHandler input = new UserInputHandler();
            SeleniumServer selServer;

            try{
                input.parseInput(args);
            } catch (Exception e) {
                Console.WriteLine(e.Message);
                Console.WriteLine(input.getManual());
                return;
            }

            String ip, browser, environment;
            String[] testCases, orderpaths;
            input.setServerAndClientParams(out ip, out browser, out environment, out testCases, out orderpaths);

            try {
                SeleniumServer.configureNewInstance(ip, browser, environment, testCases, orderpaths);
                selServer = SeleniumServer.Instance;
            } catch (Exception e) {
                Console.WriteLine(e.Message);
                Console.WriteLine("Failed to instantiate server - Terminating.");
                return;
            }



            try {
                selServer.runTests();
                Console.WriteLine("Tests have completed successfully - Terminating.");
            } catch (Exception e) {
                Console.WriteLine(e.Message);
                Console.WriteLine("Failed to run tests - Terminating.");
            } finally {
                if (selServer != null) {
                    try {
                        selServer.Dispose();
                        //explicitely call dispose to also deal with unmanaged resources, have to actually create that logic to deal with those resources
                    } catch (Exception e) {
                        Console.WriteLine(e.Message);
                    }
                }
            }
             
        }
    }
}
